package com.carlinfo.userscenter.users.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.carlinfo.usercenter.common.dao.impl.BaseDaoImpl;
import com.carlinfo.usercenter.common.util.PageInfoUtil;
import com.carlinfo.userscenter.users.dao.IAMenuDao;
import com.carlinfo.userscenter.users.pojo.AMenu;
/**
 * 角色表操作dao实现类 
 * @author jjh
 *
 */
public class AMenuDaoImpl extends BaseDaoImpl<AMenu> implements IAMenuDao
{
	
	@Override
	public int saveDao(AMenu menu)
	{
		int res = 0 ;
		System.out.println("==IAMenuDaoImpl==");
		/* 添加sql语句 */
		String sql = "insert into `a_menu`(`parentId`, `name`, `url`, `content`, `showStatus`, `leafStatus`, `status`, `createTime`, `updateTime`, `pubTime`)"
				+ " values "
				+ "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(menu.getParentId());
		paramsList.add(menu.getName());
		paramsList.add(menu.getUrl());
		paramsList.add(menu.getContent());
		paramsList.add(menu.getShowStatus());
		paramsList.add(menu.getLeafStatus());
		paramsList.add(menu.getStatus());
		paramsList.add(menu.getCreateTime());
		paramsList.add(menu.getUpdateTime());
		paramsList.add(menu.getPubTime());
		
		res = this.executeSqlUpdate(sql, paramsList);
		/**
		 * 主键被放到了list中
		 * 如何取到最后一个值呢
		 */
		menu.setId((int) paramsList.get(paramsList.size() - 1 ));
		return res ; 
	}

	@Override
	public int updateDao(AMenu menu)
	{
		/* sql语句 */
		String sql = "update `a_menu` set `parentId`= ?, `name`= ?, `url`= ?, `content`= ?, "
				+ "`showStatus`= ?, `leafStatus`= ?, `status`= ?, `createTime`= ?, `updateTime`= ?, `pubTime` = ?"
				+ " where `id` = ?" ; 
		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(menu.getParentId());
		paramsList.add(menu.getName());
		paramsList.add(menu.getUrl());
		paramsList.add(menu.getContent());
		paramsList.add(menu.getShowStatus());
		paramsList.add(menu.getLeafStatus());
		paramsList.add(menu.getStatus());
		paramsList.add(menu.getCreateTime());
		paramsList.add(menu.getUpdateTime());
		paramsList.add(menu.getPubTime());
		
		return this.executeSqlUpdate(sql, paramsList);
	}
	
	/**
	 * 
	 */
	@Override
	public int deleteDao(AMenu menu)
	{
		String sql = "delete from a_menu "
				+ " where `id` = ? " ; ;
		/* 存储数据库的条件 */
		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(menu.getId());
		return this.executeSqlUpdate(sql, paramsList);
	}

	@Override
	public AMenu findDao(Map<String, Object> condMap)
	{
		/*-----------拼装sql语句和条件-----------*/
		/**
		 * 存储条件
		 * condMap:用户输入的搜索条件
		 * paramsList;存储的是sql语句和条件
		 */
		List<Object> paramsList =  new ArrayList<Object>();
		/* sql语句和条件
		 * ？的位置和list的顺序一定要一致
		 * */
		String sql = "select * from a_menu " ;
		/* 根据id来查询 */
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			/* 您已经传了id */
			sql += "where id = ? " ;
			paramsList.add(condMap.get("id"));
		}
//		System.out.println("===AMenu==findOne===sql==" + sql) ;
//		System.out.println("===AMenu==findOne===条件===" + paramsList);
		
		return this.findOneBase(sql, paramsList, new AMenu());
	}

	
	@Override
	public List<AMenu> findCountList(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		//System.out.println("==AMenuDaoImpl========findCountList===========");
		/* sql语句的条件 */
		List<Object> paramList = new ArrayList<Object>();
		/* sql语句的条件 */
		/*-----------拼装sql语句和条件-----------*/
		/* sql语句和条件
		 * ？的位置和list的顺序一定要一致
		 * */
		String sql = "select * from a_menu where 1 = 1 " ;
		/*	
		 *	where后面跟的条件,第一个不能加and或者or
		 *解决此难题在where后面加一个永远成立的条件;1=1 
		 */
		/* 关键字 */
		if(condMap.get("keyword") != null & !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sql = sql + " and (name like ? or content like ? ) " ;
			/* 有几个？就放几个条件 */
			paramList.add("%" + condMap.get("keyword") + "%");
			paramList.add("%" + condMap.get("keyword") + "%");
		}
		
		/* 状态 */
		if(condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql = sql + " and status = ? " ;
			paramList.add(condMap.get("status"));
		}
		
		/* 时间范围 
		 * 从map中取出的值是Object,想判断一下,是不是Date对象
		 * 怎么判断:A是不是B的一个对象呢(A是对象,B是类)
		 * */
		if(condMap.get("stTime") instanceof Date && condMap.get("edTime") instanceof Date)
		{
			sql = sql + " and pubTime > ? and pubTime = ? " ;
			paramList.add(condMap.get("stTime"));
			paramList.add(condMap.get("edTime"));
		}
		
		if(condMap.get("parentId") != null && !"".equalsIgnoreCase(condMap.get("parentId") + ""))
		{
			sql = sql + " and parentId = ? " ;
			paramList.add(condMap.get("parentId"));
		}
		
		/* 默认都有orderby */
		if("iddesc".equalsIgnoreCase(condMap.get("orderby") + ""))
		{
			sql = sql + " order by id desc " ;
		}else if("idasc".equalsIgnoreCase(condMap.get("idasc") + ""))
		{
			sql = sql + " order by id asc " ;
		}else
		{
			sql = sql + " order by id asc " ;
		}
		
		//System.out.println("===AMenu==findOne===sql==" + sql) ;
		
		/*分页查询
		 * 不分页查询
		 * 搜索查询
		 * 
		 */
		if(pageInfoUtil != null)
		{
			/* 分页查询 */
			return this.findByPage(pageInfoUtil, sql, paramList, new AMenu());
		}
		/* 不分页查询 */
		return this.find(sql, paramList, new AMenu());
	}
}
